use crate::db::connect_to_db;
use anyhow::Result;
use teloxide::prelude2::*;
use uuid::Uuid;
pub async fn delete(bot: AutoSend<Bot>, q: CallbackQuery, data: &str) -> Result<()> {
    let user_id = q.from.id;
    let user = get_user(user_id).await?;
    delete_post(data, user.id).await?;
    bot.answer_callback_query(q.id).text("Deleted!").await?;
    if let Some(message) = q.message {
        println!("test");
        bot.delete_message(message.chat.id, message.id).await?;
    }
    Ok(())
}
use crate::helpers::uuid_from_str;
use mongodb::bson::doc;

use super::{get_user, UserPost};
async fn delete_post(post_id: &str, user_id: Uuid) -> Result<()> {
    let bin_id = uuid_from_str(post_id)?;
    let db = connect_to_db().await?;
    let coll = db.collection::<UserPost>("userposts");
    let query = doc! {
        "user_id": user_id,
        "post_id": &bin_id
    };
    /*let update = doc! {
        "$pop": {
            "posts":"-$[elem]",
        }
    };*/
    /*let array_filters = vec![doc! {
        "elem.post_id": bin_id,
    }];*/
    /*let options = UpdateOptions::builder()
    .array_filters(array_filters)
    .build();*/
    //coll.update_one(query, update, options).await?;
    coll.delete_one(query, None).await?;
    Ok(())
}

/*async fn delete_post(post_id: &str, user_id: i64) -> Result<()> {
    let bin_id = uuid(post_id)?;
    let db = connect_to_db().await?;
    let coll = db.collection::<User>("users");
    let match_aggr = doc!{
        "$match": {
        "telegram_id": user_id,
        "posts.post_id": &bin_id
    }
};

    let pipeline = [];
    let options = None;
    coll.aggregate(pipeline, options).await?;
    Ok(())
}*/
